﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;

namespace HRdex_Final.Jobs
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PNapplicant.Visible = false;
        }
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");
        protected void gvApplicants_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    // Get reference to button field in the gridview.  
                    LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
                    string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);
                    e.Row.Style["cursor"] = "hand";
                    e.Row.Attributes["onclick"] = _jsSingle;
                }
            }  
        }

        protected void gvApplicants_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvApplicants.Visible = false;
            PNapplicant.Visible = true;
            GridViewRow selectedRow = gvApplicants.SelectedRow;
            lblname.Text = selectedRow.Cells[1].Text;
            lblno.Text = selectedRow.Cells[2].Text;
            lblemail.Text = selectedRow.Cells[3].Text;
            lblpos.Text = selectedRow.Cells[4].Text;
            lblresume.Text = selectedRow.Cells[5].Text;
        }


        protected override void Render(HtmlTextWriter writer)
        {
            foreach (GridViewRow row in gvApplicants.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    ClientScript.RegisterForEventValidation(((LinkButton)row.Cells[0].Controls[0]).UniqueID, "Select$" + row.RowIndex);
                }
            }
            base.Render(writer);
        }


        protected void btnRsume_Click(object sender, EventArgs e)
        {
            string announcementID = gvApplicants.SelectedDataKey.Value.ToString();
            string strQuery = "SELECT resume, resName, resType FROM formApplicant WHERE ApplicantID = " + announcementID;
            SqlCommand cmd = new SqlCommand(strQuery);
            DataTable dt = GetData(cmd);
            if (dt != null)
            {
                download(dt);
            }
        }

        private DataTable GetData(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;
            try
            {
                conn.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
                return dt;

            }
            catch
            {
                return null;
            }
            finally
            {
                conn.Close();
                sda.Dispose();
                conn.Dispose();
            }

        }
        private void download(DataTable dt)
        {

            Byte[] bytes = (Byte[])dt.Rows[0]["resume"];
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = dt.Rows[0]["resType"].ToString();
            Response.AddHeader("content-disposition", "attachment;FileName="
            + dt.Rows[0]["resName"].ToString());
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }


    }
}